package com.asset.salesforce.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Country;
import com.asset.salesforce.domain.DeletedAccount;
import com.asset.salesforce.domain.DeletedContact;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Role;
import com.asset.salesforce.domain.User;
//import com.asset.salesforce.domain.FollowType;
//import com.asset.salesforce.domain.Industry;
import com.asset.salesforce.domain.Lead;
//import com.asset.salesforce.domain.LeadStatus;
//import com.asset.salesforce.domain.LeadSource;
import com.asset.salesforce.domain.Post;
//import com.asset.salesforce.domain.Priority;
//import com.asset.salesforce.domain.Status;
import com.asset.salesforce.domain.UpdateChatter;
import com.asset.salesforce.enums.FOLLOWTYPE;
import com.asset.salesforce.enums.INDUSTRY;
import com.asset.salesforce.enums.TYPE;
import com.asset.salesforce.enums.UPDATETYPE;
import com.asset.salesforce.enums.VIEWTYPE;
import com.asset.salesforce.form.AccountData;
import com.asset.salesforce.form.AccountForm;
import com.asset.salesforce.form.ContactData;
import com.asset.salesforce.form.DataTable;
//import com.asset.salesforce.domain.UpdateType;
import com.asset.salesforce.form.ContactForm;
import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CheckingUtilsService;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.DeletedContactService;
import com.asset.salesforce.service.SearchService;
import com.asset.salesforce.service.UserShiroService;
//import com.asset.salesforce.service.FollowTypeService;
//import com.asset.salesforce.service.IndusteryService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.LeadService;
//import com.asset.salesforce.service.LeadSourceService;
//import com.asset.salesforce.service.LeadStatusService;
import com.asset.salesforce.service.PostService;
import com.asset.salesforce.service.UpdateChatterService;
//import com.asset.salesforce.service.UpdateTypeService;
//import com.asset.salesforce.service.ViewTypeService;
import com.asset.salesforce.converter.ContactConverter;

@Controller
public class ContactController {
// @Autowired
// private LeadSourceService leadSourceService;
 
// @Autowired
// private IndusteryService industeryService;
 
 @Autowired
 private CountryService countryService;
 
//@Autowired
//private LeadStatusService leadStatusService;
@Autowired
private AccountService accountService;
@Autowired
private ContactService contactService;

@Autowired
private DeletedContactService deletedContactService;

@Autowired
private CheckingUtilsService checkingUtilsService;

//@Autowired
//private FollowTypeService followTypeService;

//@Autowired
//private UpdateTypeService updateTypeService;

@Autowired
private UpdateChatterService updateChatterService;

@Autowired
private PostService postService;

@Autowired
private JoinRequestService joinRequestService;

//@Autowired
//private ViewTypeService viewTypeService;

@Autowired
private LeadService leadService ;

@Autowired
private CalendarService calendarService;

@Autowired
private SearchService searchService;
@Autowired
private UserShiroService userShiroService;

	
	@RequestMapping(value="/getAllContacts", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public DataTable<ContactData> getAllContacts(HttpSession session){
		DataTable<ContactData> dataTable = new DataTable<ContactData>();
		Employee userDetails = (Employee) session.getAttribute("userDetails");
		List<Contact>contacts = new ArrayList<Contact>();
		if (userDetails.getIsOwner()==true){
			contacts = contactService.getAllContactbyCompany(userDetails.getCompany().getCompanyName());
		}
		else{
			contacts = contactService.getAllContactByEmpId(userDetails.getId());
		}
		//List<Contact>contacts = contactService.getAllContactByEmpId(userDetails.getId());
		List<ContactData>contactData = new ArrayList<ContactData>();
		for (Contact contact : contacts) {
			ContactData data = new ContactData();
			data.setId(contact.getId());
			data.setContactName(contact.getfName());
			contact.setIsFollowed(checkingUtilsService.checkIfCurrentFollowThatContact(userDetails, contact));
			if(contact.getIsFollowed()==true){
				data.setIsFollowed(contact.getId().toString()+"/"+"true");
			}
			else{
				data.setIsFollowed(contact.getId().toString()+"/"+"false");
			}
			if(contact.getMobile()!=null&&!contact.getMobile().equalsIgnoreCase("")){
				data.setMobile(contact.getMobile());
			}
			if(contact.getViewType()!=null&&!contact.getViewType().equalsIgnoreCase("")){
				if(contact.getViewType().equals("lead")){
					Lead lead = leadService.getLeadById(contact.getParentId());
					if(lead!=null){
						data.setCompanyName(lead.getCompanyName());
					}
				}
				else
					if(contact.getViewType().equals("account")){
						Account account = accountService.getAccount(contact.getParentId());
						if(account!=null){
							data.setCompanyName(account.getCompanyName());
						}
					}
			}			
			contactData.add(data);
		}
		dataTable.setAaData(contactData);
		return dataTable;
	}

	@RequestMapping(value="/new-contact",method=RequestMethod.GET)
	public String getNewContact(Model model,HttpSession session, ModelMap modelMap){
		if (SecurityUtils.getSubject().hasRole("contactAdd")){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		model.addAttribute("userDetails",userDetails);
		ContactForm  contactForm = new ContactForm();
		modelMap.addAttribute("contactForm", contactForm);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "new-contact";
		}
		else{
			return "accessDenied";
		}
	}
	@RequestMapping(value="/edit-contact",method=RequestMethod.GET)
	public String editContact(Model model ,HttpSession session,ModelMap modelMap,
			HttpServletRequest request,HttpServletResponse response){
			if (SecurityUtils.getSubject().hasRole("contactEdit")){
				Integer id = Integer.parseInt(request.getParameter("id").toString());
			Contact contact = contactService.getContactById(id);
			ContactConverter contactConverter  = new ContactConverter();
			ContactForm contactForm = contactConverter.convertContact(contact);
			
			if(contact.getViewType() != null){
				model.addAttribute("viewType", contact.getViewType());
				if(contact.getViewType().equalsIgnoreCase("lead")){
					Lead lead = leadService.getLeadById(contact.getParentId());
					if(lead.getfName()!=null&&!(lead.getfName().equalsIgnoreCase(""))&&lead.getlName()!=null&&(!lead.getlName().equalsIgnoreCase(""))){
						contactForm.setParentName(lead.getfName()+" "+lead.getlName());
					}	
				}else if(contact.getViewType().equalsIgnoreCase("account")){
					Account account = accountService.getAccount(contact.getParentId());
					contactForm.setParentName(account.getAccountName());
				}
			}
			modelMap.addAttribute("contactForm", contactForm);
			model.addAttribute("op", "edit");
			model.addAttribute("contact", contact);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "edit-contact";
			}
			else{return "accessDenied";}
	}
//	@RequestMapping(value="/new-contact" ,method=RequestMethod.GET)
//	public String getNewContact(Model model ,HttpSession session,ModelMap modelMap,
//			HttpServletRequest request,HttpServletResponse response){
//		if(null ==session.getAttribute("userDetails") ){
//			return "redirect:/s/login.html";
//		}
//		
//		//response.setCharacterEncoding("UTF-8");
//		System.out.println("response encoding"+response.getCharacterEncoding());
//		System.out.println("request encoding"+request.getCharacterEncoding());
//		Object id = request.getParameter("id");
//		if(id != null){
//			if (SecurityUtils.getSubject().hasRole("contactEdit")){
//			Contact contact = contactService.getContactById(Integer.parseInt(id.toString()));
//			ContactConverter contactConverter  = new ContactConverter();
//			ContactForm contactForm = contactConverter.convertContact(contact);
//			
//			if(contact.getViewType() != null){
//				model.addAttribute("viewType", contact.getViewType());
//				if(contact.getViewType().equalsIgnoreCase("lead")){
//					Lead lead = leadService.getLeadById(contact.getParentId());
//					contactForm.setParentName(lead.getfName()+" "+lead.getlName());
//				}else if(contact.getViewType().equalsIgnoreCase("account")){
//					Account account = accountService.getAccount(contact.getParentId());
//					contactForm.setParentName(account.getAccountName());
//				}
//			}
//			modelMap.addAttribute("contactForm", contactForm);
//			model.addAttribute("op", "edit");
//			model.addAttribute("contact", contact);
//			return "edit-contact";
//			}
//			else{return "accessDenied";}
//		}else{
//			if (SecurityUtils.getSubject().hasRole("contactAdd")){
//		ContactForm  contactForm = new ContactForm();
//		modelMap.addAttribute("contactForm", contactForm);
//		model.addAttribute("op", "new");
//			}
//			else{return "accessDenied";}
//		}
//		joinRequestService.getJoinRquestNo(session, model);
//		calendarService.getAppointmentEventsNo(session, model);
//		calendarService.getCallEventssNo(session, model);
//		return "new-contact";
//	}
	
	@RequestMapping(value="/new-contact" ,method=RequestMethod.POST)
	public String postNewContact(Model model ,HttpSession session,ModelMap modelMap,
			@Valid @ModelAttribute("contactForm") ContactForm contactForm,BindingResult result,HttpServletRequest request,
			HttpServletResponse response){
		Integer parentId=contactForm.getParentId();
		String type=contactForm.getType();
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		System.out.println("request encoding"+request.getCharacterEncoding());
			//response.setCharacterEncoding("UTF-8");
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		System.out.println("response encoding"+response.getCharacterEncoding());
		System.out.println("request encoding"+request.getCharacterEncoding());
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		model.addAttribute("userDetails", userDetails);
		List<Contact> contacts = contactService.getAllContactByEmpId(userDetails.getId());
		if(contacts.size()>0){
			for (Contact contact : contacts) {
				if(contactForm.getPhone()!=null&&!contactForm.getPhone().equalsIgnoreCase("")){
					if(contact.getPhone()!=null&&!contact.getPhone().equalsIgnoreCase("")){
						if(contact.getPhone().equalsIgnoreCase(contactForm.getPhone())){
							result.rejectValue("phone", "phone","this phone already exists");
						}
					}
				}
				if(contactForm.getMobile()!=null&&!contactForm.getMobile().equalsIgnoreCase("")){
					if(contact.getMobile()!=null&&!contact.getMobile().equalsIgnoreCase("")){
						if(contact.getMobile().equalsIgnoreCase(contactForm.getMobile())){
							result.rejectValue("mobile", "mobile","this mobile already exists");
						}
					}
				}
			}
		}

		if(result.hasErrors()){
			if(contactForm.getId() != null){
				System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>....."+result.getAllErrors().size());
				model.addAttribute("contactForm", contactForm);
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
				return "edit-contact";
			}
			else{
				System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>....."+result.getAllErrors().size());
				model.addAttribute("contactForm", contactForm);
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
				return "new-contact";
			}
		}
		
		if(contactForm.getId() != null){
			Contact contact2 = contactService.getContactById(contactForm.getId());
			contact2.setParentId(parentId);
			if(type.startsWith("(l")){
				contact2.setViewType(VIEWTYPE.Lead.getName());
			}else if(type.startsWith("(a")){
				contact2.setViewType(VIEWTYPE.Account.getName());
			}
			contact2.setCreator(userDetails);
			contact2.setEmail(contactForm.getEmail());
			contact2.setfName(contactForm.getfName());
			contact2.setIsDeleted(false);
			contact2.setlName(contactForm.getlName());
			contact2.setMobile(contactForm.getMobile());
			contact2.setPhone(contactForm.getPhone());
			contact2.setTitle(contactForm.getTitle());
			contact2.setParentName(contactForm.getParentName());
			contact2.setParentId(parentId);
			contact2.setDateModified(new Date());

			System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "+parentId);
			contactService.saveOrUpdate(contact2);
			
			//save post to view on chatter page
//			FollowType followType = followTypeService.getFollowTypeById(1);
//					UpdateType updateType = updateTypeService.getById(1);
					UpdateChatter updateChatter = new UpdateChatter();
					updateChatter.setAction_emp(userDetails);
					updateChatter.setDate(new Date());
					updateChatter.setFollowedId(contact2.getId());
					updateChatter.setFollowType(FOLLOWTYPE.Account.getType());
					updateChatter.setUpdateType(UPDATETYPE.Edited.getName());
					
//					Post post = new Post();
//					post.setPostDate(new Date());
//					post.setPoster(userDetails);
//					String postText = "<a href='/sales/s/viewContact.html?id="+contact2.getId()+"'>i edited Contact Name:"+contact2.getfName() +" "+contact2.getlName()+"</a>";
//					post.setPostText(postText);
//					postService.addPost(post);	
//					updateChatter.setPost(post);
//					updateChatterService.add(updateChatter);

		}else {
			Contact contact = new Contact();
			if(contactForm.getParentName() != null && !contactForm.getParentName().equalsIgnoreCase("")){
				//contact.setParentId(parentId);
				contact.setParentName(contactForm.getParentName());
				if(type.startsWith("(l")){
					contact.setViewType(VIEWTYPE.Lead.getName());
					contact.setParentId(parentId);
					contact.setParentName(contactForm.getParentName());
				}else if(type.startsWith("(a")){
					contact.setViewType(VIEWTYPE.Account.getName() );
					contact.setParentId(parentId);
					contact.setParentName(contactForm.getParentName());
				}
			}
			contact.setCreator((Employee) session.getAttribute("userDetails"));
			contact.setDate(new Date());
			if(contactForm.getEmail() != null && !contactForm.getEmail().equalsIgnoreCase("")){
				contact.setEmail(contactForm.getEmail());
			}
			if(contactForm.getMobile() != null && !contactForm.getMobile().equalsIgnoreCase("")){
				contact.setMobile(contactForm.getMobile());
			}
			if(contactForm.getPhone() != null && !contactForm.getPhone().equalsIgnoreCase("")){
				contact.setPhone(contactForm.getPhone());
			}
			if(contactForm.getTitle() != null && !contactForm.getTitle().equalsIgnoreCase("")){
				contact.setTitle(contactForm.getTitle());
			}
			contact.setfName(contactForm.getfName());
			contact.setlName(contactForm.getlName());
			contact.setIsDeleted(false);
			contact.setParentName(contactForm.getParentName());
			Date date = new Date();
			contact.setDateCreated(date);
			contact.setDateModified(date);
			Lead lead = leadService.getLeadById(parentId);
			contact.setLeadId(lead);
			contactService.addContact(contact);
			//save post to view on chatter page
//			FollowType followType = followTypeService.getFollowTypeById(1);
//					UpdateType updateType = updateTypeService.getById(5);
					UpdateChatter updateChatter = new UpdateChatter();
					updateChatter.setAction_emp(userDetails);
					updateChatter.setDate(new Date());
					updateChatter.setFollowedId(contact.getId());
					updateChatter.setFollowType(FOLLOWTYPE.Account.getType());
					updateChatter.setUpdateType(UPDATETYPE.Added.getName());
					
					Post post = new Post();
					post.setPostDate(new Date());
					post.setPoster(userDetails);
					String postText = "<a href='/sales/s/viewContact.html?id="+contact.getId()+"'>i created Contact Name:"+contact.getfName() +" "+contact.getlName()+"</a>";
					post.setPostText(postText);
					postService.addPost(post);	
					updateChatter.setPost(post);
					updateChatterService.add(updateChatter);

		}
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
				return "redirect:/s/contacts.html";
	}
	
	@RequestMapping(value="/contacts" ,method=RequestMethod.GET)
	public String getGetContacts(Model model ,HttpSession session,
			@RequestParam(value="index",required=false)Integer index,
			@RequestParam(value="pages",required=false)Integer pages,
			@RequestParam(value="count",required=false)Integer count){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		if(count ==null){
			count = 5;
		}
		Employee userDetails = (Employee)session.getAttribute("userDetails") ;
		model.addAttribute("userDetails",userDetails);
		List<Contact> contacts = new ArrayList<Contact>();
		if(index != null){
			contacts = contactService.getAllContactsForCompany(index,userDetails.getId(),count);
			model.addAttribute("index", index);
			model.addAttribute("pages", pages);
			model.addAttribute("count", count);
		}else{
			Integer size = contactService.getAllContactForCompanySize(userDetails.getId());
			Integer numOfPages = size/count;
			if(size % count > 0){
				numOfPages++;
			}
			contacts = contactService.getAllContactsForCompany(0,userDetails.getId(),count);
			model.addAttribute("index", 0);
			model.addAttribute("pages", numOfPages);
			model.addAttribute("count", count);
		}			
		for (Contact contact : contacts) {
			contact.setIsFollowed(checkingUtilsService.checkIfCurrentFollowThatContact(userDetails, contact));
		}
		System.out.println("000000000000000000000000000000000000000"+contacts.size());
		List<User> users = userShiroService.getAllUsers(userDetails);
		//List<Role> roles = roleShiroService.getAllRoles();
		User user = users.get(0);
		List<Role>userRoles=user.getRoles();
		
		List<String>roles = new ArrayList<String>();
		for (Role role : userRoles) {
			roles.add(role.getName());
		}
		model.addAttribute("userRoles", roles);
		model.addAttribute("contacts", contacts);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "contacts";
	}
	@RequestMapping(value="/deleteContact" ,method=RequestMethod.GET)
	public String getdeleteContact(Model model ,HttpSession session,HttpServletRequest request){
		if (SecurityUtils.getSubject().hasRole("contactDelete")){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		
		Contact contact = contactService.getContactById(Integer.parseInt(request.getParameter("id").toString()));
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		//save post to view on chatter page
//		FollowType followType = followTypeService.getFollowTypeById(1);
//				UpdateType updateType = updateTypeService.getById(5);
				UpdateChatter updateChatter = new UpdateChatter();
				updateChatter.setAction_emp(userDetails);
				updateChatter.setDate(new Date());
				updateChatter.setFollowedId(contact.getId());
				updateChatter.setFollowType(FOLLOWTYPE.Account.getType());
				updateChatter.setUpdateType(UPDATETYPE.Added.getName());
				
//				Post post = new Post();
//				post.setPostDate(new Date());
//				post.setPoster(userDetails);
//				String postText = "<a href='/sales/s/viewContact.html?id="+contact.getId()+"'>i deleted that Contact Name:"+contact.getfName() +" "+contact.getlName()+"</a>";
//				post.setPostText(postText);
//				postService.addPost(post);	
//				updateChatter.setPost(post);
//				updateChatterService.add(updateChatter);
				
				contact.setIsDeleted(true);
				contactService.saveOrUpdate(contact);
				DeletedContact deletedContact = new DeletedContact();
				deletedContact.setDate(new Date());
				deletedContact.setDeleted(contact);
				deletedContact.setDeleter((Employee)session.getAttribute("userDetails"));
				deletedContactService.addDeletedContact(deletedContact);
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
		return "redirect:/s/contacts.html";
		}
		else{
			return "accessDenied";
		}
	}
	@RequestMapping(value="/viewContact" ,method=RequestMethod.GET)
	public String getViewContact(Model model ,HttpSession session,HttpServletRequest request,ModelMap modelMap){
		if (SecurityUtils.getSubject().hasRole("contactView")){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Contact contact = contactService.getContactById(Integer.parseInt(request.getParameter("id").toString()));
		ContactConverter contactConverter = new ContactConverter();
		ContactForm contactForm = contactConverter.convertContact(contact);
		if(contact.getViewType() != null){
			model.addAttribute("viewType", contact.getViewType());
			if(contact.getViewType().equalsIgnoreCase("lead")){
				Lead lead = leadService.getLeadById(contact.getParentId());
				contactForm.setParentName(lead.getfName()+" "+lead.getlName());
				modelMap.addAttribute("lead", lead);
			}else if(contact.getViewType().equalsIgnoreCase("account")){
				Account account = accountService.getAccount(contact.getParentId());
				contactForm.setParentName(account.getAccountName());
				modelMap.addAttribute("account", account);
			}
		}
		modelMap.addAttribute("contactForm", contactForm);
		//get number of pending requests
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		joinRequestService.getJoinRquestNo(session, model);
		return "viewContact";
		}
		else{
			return "accessDenied";
		}
	}
	
//	@RequestMapping(value="/searchContacts" ,method = RequestMethod.POST)
//	public  String searchContacts(String name,HttpSession session,Model model,
//			@RequestParam(value="query",required=false)String query) {
//		Employee userDetails = (Employee)session.getAttribute("userDetails");
//		//List<Contact>contacts = new ArrayList<Contact>();
//		//contacts = contactService.getAllContactByName(query);
//		List<Contact> contacts = new ArrayList<Contact>();
//		contacts = searchService.searchContact(query,userDetails.getId());
//		if(contacts != null){
//			
//			model.addAttribute("contacts", contacts);
//		}
//		return "search_contacts";
//	}
	
	@RequestMapping(value="/searchContacts" ,method = RequestMethod.POST)
	public  String searchContact(String name,HttpSession session,Model model,
			@RequestParam(value="query",required=false)String query) {
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		List<Contact> contacts = contactService.getAllContactByName(query,userDetails.getCompany().getCompanyName());
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+contacts.size());
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:/s/login.html";
			}
			model.addAttribute("contacts", contacts);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "search_contacts";
	}
}



